package com.yisu.expressway.manager.core.service.impl;

import com.yisu.expressway.ewf.common.web.EasyUIGrid;
import com.yisu.expressway.ewf.entity.OrderMain;
import com.yisu.expressway.ewf.entity.RefundRecord;
import com.yisu.expressway.manager.core.mapper.OrderMainMapper;
import com.yisu.expressway.manager.core.mapper.RefundRecordMapper;
import com.yisu.expressway.manager.core.service.IRefundRecordService;
import com.yisu.expressway.manager.domain.request.RefundRecordRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 *
 * User: 赵杰
 * Date: 2017/5/16
 * Time: 13:21
 * Desc: 退款相关流程处理.
 */
@Service
public class RefundRecordServiceImpl implements IRefundRecordService {
    @Resource
    private RefundRecordMapper refundRecordMapper;
    @Resource
    private OrderMainMapper orderMainMapper;
    /**
     * 分页获取退款流水
     *
     * @param refundRecordRequest
     * @return
     */
    @Override
    public EasyUIGrid<RefundRecord> getRefundRecordListByPage(RefundRecordRequest refundRecordRequest) {
        java.util.List<RefundRecord> refundRecords = refundRecordMapper.getRefundRecordListByPage(refundRecordRequest);
        EasyUIGrid<RefundRecord> gridData = new EasyUIGrid<>();
        gridData.setTotal(refundRecordRequest.getTotalRecords());
        gridData.setRows(refundRecords);
        return gridData;
    }

    /**
     * 通过主键获取退款信息实体
     *
     * @param primaryKey 主键信息
     * @return
     */
    @Override
    public RefundRecord getModelByPrimaryKey(long primaryKey) {
        return refundRecordMapper.selectByPrimaryKey(primaryKey);
    }

    /**
     * 退款成功
     *
     * @param updateModel 退款更新实体
     * @return
     */
    @Override
    public boolean refundSuccess(RefundRecord updateModel) {
        try {
            if (refundRecordMapper.updateByPrimaryKeySelective(updateModel) > 0) {
                OrderMain updateOrderMain = new OrderMain();
                updateOrderMain.setOrderId(updateModel.getOrderId());
                updateOrderMain.setOrderStatus(OrderMain.OrderStatus.CANCEL_ORDER.code);
                orderMainMapper.updateOrderStatus(updateOrderMain);
            }
        } catch (Exception ex) {
            return false;
        }

        return true;
    }
}
